<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="对应的订单ID" prop="orderId">
        <el-input
            v-model="queryParams.orderId"
            placeholder="请输入对应的订单ID"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请原因" prop="asCause">
        <el-input
            v-model="queryParams.asCause"
            placeholder="请输入申请原因"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退款申请时间" prop="asCreateTime">
        <el-date-picker clearable
                        v-model="queryParams.asCreateTime"
                        type="date"
                        value-format="YYYY-MM-DD"
                        placeholder="请选择退款申请时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="退款完成时间" prop="asSuccessTime">
        <el-date-picker clearable
                        v-model="queryParams.asSuccessTime"
                        type="date"
                        value-format="YYYY-MM-DD"
                        placeholder="请选择退款完成时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="退款申请金额" prop="asMoney">
        <el-input
            v-model="queryParams.asMoney"
            placeholder="请输入退款申请金额"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请用户ID" prop="userId">
        <el-input
            v-model="queryParams.userId"
            placeholder="请输入申请用户ID"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商家ID" prop="businessId">
        <el-input
            v-model="queryParams.businessId"
            placeholder="请输入商家ID"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="拒绝/终止原因" prop="refundReason">
        <el-input
            v-model="queryParams.refundReason"
            placeholder="请输入拒绝/终止原因"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退款凭证" prop="refundProof">
        <el-input
            v-model="queryParams.refundProof"
            placeholder="请输入退款凭证"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['zmhshop:afterSale:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['zmhshop:afterSale:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['zmhshop:afterSale:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['zmhshop:afterSale:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="afterSaleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="售后单号" align="center" prop="asId" />
      <el-table-column label="对应的订单单号" align="center" prop="orderId" />
      <el-table-column label="售后类型(1-退款,2-退货)" align="center" prop="asType" />
      <el-table-column label="售后状态(1-申请中,2-通过售后,3-拒绝售后,4-卖家终止售后,5-待平台退款,6-完成售后,7-买家取消售后)" align="center" prop="asStatus" />
      <el-table-column label="申请原因" align="center" prop="asCause" />
      <el-table-column label="退款申请时间" align="center" prop="asCreateTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.asCreateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="退款完成时间" align="center" prop="asSuccessTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.asSuccessTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="退款申请金额" align="center" prop="asMoney" />
      <el-table-column label="申请用户ID" align="center" prop="userId" />
      <el-table-column label="商家ID" align="center" prop="businessId" />
      <el-table-column label="拒绝/终止原因" align="center" prop="refundReason" />
      <el-table-column label="退款凭证" align="center" prop="refundProof" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="View" @click="handleView(scope.row)">查看</el-button>
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['zmhshop:afterSale:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['zmhshop:afterSale:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改退款对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="afterSaleRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="对应的订单ID" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入对应的订单ID" />
        </el-form-item>
        <el-form-item label="申请原因" prop="asCause">
          <el-input v-model="form.asCause" placeholder="请输入申请原因" />
        </el-form-item>
        <el-form-item label="退款申请时间" prop="asCreateTime">
          <el-date-picker clearable
                          v-model="form.asCreateTime"
                          type="date"
                          value-format="YYYY-MM-DD"
                          placeholder="请选择退款申请时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="退款完成时间" prop="asSuccessTime">
          <el-date-picker clearable
                          v-model="form.asSuccessTime"
                          type="date"
                          value-format="YYYY-MM-DD"
                          placeholder="请选择退款完成时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="退款申请金额" prop="asMoney">
          <el-input v-model="form.asMoney" placeholder="请输入退款申请金额" />
        </el-form-item>
        <el-form-item label="申请用户ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入申请用户ID" />
        </el-form-item>
        <el-form-item label="商家ID" prop="businessId">
          <el-input v-model="form.businessId" placeholder="请输入商家ID" />
        </el-form-item>
        <el-form-item label="拒绝/终止原因" prop="refundReason">
          <el-input v-model="form.refundReason" placeholder="请输入拒绝/终止原因" />
        </el-form-item>
        <el-form-item label="退款凭证" prop="refundProof">
          <el-input v-model="form.refundProof" placeholder="请输入退款凭证" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="AfterSale">
import { listAfterSale, getAfterSale, delAfterSale, addAfterSale, updateAfterSale } from "@/api/zmhshop/afterSale"

const { proxy } = getCurrentInstance()

const afterSaleList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orderId: null,
    asType: null,
    asStatus: null,
    asCause: null,
    asCreateTime: null,
    asSuccessTime: null,
    asMoney: null,
    userId: null,
    businessId: null,
    refundReason: null,
    refundProof: null
  },
  rules: {
    orderId: [
      { required: true, message: "对应的订单ID不能为空", trigger: "blur" }
    ],
    asType: [
      { required: true, message: "售后类型(1-退款,2-退货)不能为空", trigger: "change" }
    ],
    asStatus: [
      { required: true, message: "售后状态(1-申请中,2-通过售后,3-拒绝售后,4-卖家终止售后,5-待平台退款,6-完成售后,7-买家取消售后)不能为空", trigger: "change" }
    ],
    asCreateTime: [
      { required: true, message: "退款申请时间不能为空", trigger: "blur" }
    ],
    asMoney: [
      { required: true, message: "退款申请金额不能为空", trigger: "blur" }
    ],
    userId: [
      { required: true, message: "申请用户ID不能为空", trigger: "blur" }
    ],
    businessId: [
      { required: true, message: "商家ID不能为空", trigger: "blur" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询退款列表 */
function getList() {
  loading.value = true
  listAfterSale(queryParams.value).then(response => {
    afterSaleList.value = response.rows.filter(item => item.orderId === 2)
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    asId: null,
    orderId: null,
    asType: null,
    asStatus: null,
    asCause: null,
    asCreateTime: null,
    asSuccessTime: null,
    asMoney: null,
    userId: null,
    businessId: null,
    refundReason: null,
    refundProof: null
  }
  proxy.resetForm("afterSaleRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.asId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加退款"
}

/** 查看按钮操作 */
function handleView(row) {
  const asId = row.asId
  if (!asId) return
  proxy.$router.push({ path: '/refund/detail', query: { asId } })
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _asId = row.asId || ids.value
  getAfterSale(_asId).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改退款"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["afterSaleRef"].validate(valid => {
    if (valid) {
      if (form.value.asId != null) {
        updateAfterSale(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addAfterSale(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _asIds = row.asId || ids.value
  proxy.$modal.confirm('是否确认删除退款编号为"' + _asIds + '"的数据项？').then(function() {
    return delAfterSale(_asIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('zmhshop/afterSale/export', {
    ...queryParams.value
  }, `afterSale_${new Date().getTime()}.xlsx`)
}

getList()
</script>
